package cn.tpshion.vm.controller;

import cn.tpshion.vm.common.model.Result;
import cn.tpshion.vm.common.utils.AssertUtil;
import cn.tpshion.vm.domain.dto.task.*;
import cn.tpshion.vm.domain.entity.TaskHis;
import cn.tpshion.vm.domain.entity.TaskNamespace;
import cn.tpshion.vm.domain.entity.TaskTemplate;
import cn.tpshion.vm.domain.vo.TaskInfoVO;
import cn.tpshion.vm.domain.vo.TaskVO;
import cn.tpshion.vm.service.TaskService;
import com.mybatisflex.core.paginate.Page;
import jakarta.annotation.Resource;
import jakarta.validation.Valid;
import org.springframework.web.bind.annotation.*;
import java.util.List;

@RestController
@RequestMapping("/task")
public class TaskController {

    @Resource
    private TaskService taskService;

    @GetMapping("/taskNamespaceList")
    public Result<List<TaskNamespace>> taskNamespaceList() {
        return Result.ok(taskService.getTaskNamespaceList());
    }

    @PostMapping("/addNamespace")
    public Result<TaskNamespace> addNamespace(@Valid @RequestBody AddNamespaceDTO dto){
        return Result.ok(taskService.addNamespace(dto));
    }

    @DeleteMapping("/deleteNamespace")
    public Result deleteNamespace(@RequestParam String id){
        AssertUtil.isBlank(id, "删除失败");
        taskService.deleteNamespace(id);
        return Result.ok();
    }

    @PostMapping("/taskList")
    public Result<Page<TaskVO>> taskList(@RequestBody QueryTaskDTO dto){
        return Result.ok(taskService.taskList(dto));
    }

    @PostMapping("/addTask")
    public Result addTask(@Valid @RequestBody AddTaskDTO dto){
        taskService.addTask(dto);
        return Result.ok();
    }

    @PostMapping("/deleteTask")
    public Result deleteTask(@RequestBody DeleteTaskDTO dto){
        AssertUtil.isEmpty(dto.getList(), "删除失败");
        taskService.deleteTask(dto);
        return Result.ok();
    }

    @GetMapping("/taskInfo")
    public Result<TaskInfoVO> taskInfo(@RequestParam String id, @RequestParam Boolean edit){
        return Result.ok(taskService.getTaskInfo(id, edit));
    }

    @PostMapping("/saveFlowData")
    public Result saveFlowData(@Valid @RequestBody TaskInfoDTO dto){
        taskService.saveFlowData(dto);
        return Result.ok();
    }

    @PostMapping("/updateTaskData")
    public Result updateTaskData(@Valid @RequestBody TaskInfoDTO dto){
        taskService.updateTaskData(dto);
        return Result.ok();
    }

    @GetMapping("/runTask")
    public Result runTask(@RequestParam String taskId){
        taskService.runTask(taskId);
        return Result.ok();
    }

    @GetMapping("stopTask")
    public Result stopTask(@RequestParam String taskId){
        taskService.stopTask(taskId);
        return Result.ok();
    }

    @PostMapping("/hisList")
    public Result<Page<TaskHis>> hisList(@Valid @RequestBody QueryHisDTO dto){
        return Result.ok(taskService.hisList(dto));
    }

    @GetMapping("/hisLog")
    public Result<List<String>> hisLog(@RequestParam String taskId, @RequestParam Long batchId){
        return Result.ok(taskService.hisLog(taskId, batchId));
    }

    @GetMapping("/branchList")
    public Result<List<String>> branchList(@RequestParam String taskId){
        return Result.ok(taskService.branchList(taskId));
    }

    @PostMapping("/saveTemplate")
    public Result saveTemplate(@Valid @RequestBody TaskTemplateDTO dto){
        taskService.saveTemplate(dto);
        return Result.ok();
    }

    @DeleteMapping("deleteTemplate")
    public Result deleteTemplate(@RequestParam Long id){
        taskService.deleteTemplate(id);
        return Result.ok();
    }

    @GetMapping("templateList")
    public Result<List<TaskTemplate>> templateList(){
        return Result.ok(taskService.templateList());
    }
}
